home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Human Interface Toolbox / PopUpMenuWithCurFont / PopUpMenuWithCurFont.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.9 KB  |  239 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        PopUpMenuWithCurFont.c
  3.  
  4.     Contains:    PopUpMenuSelectWithCurFont demonstrates which low memory globals
  5.                 (and possibly even nastier things) one must twiddle in order to
  6.                 control the font used by MDEF 0 during PopUpMenuSelect. Note that
  7.                 aside from the compatibility code associated with HOSTED_BY_FONT_MISCREANT,
  8.                 this code roughly parallels what the popup menu CDEF (ID 63) does.
  9.                 If at all possible you should make use of the CDEF, because when
  10.                 Engineering breaks the low memory trick, we can fix the CDEF, but
  11.                 we can't fix your code.
  12.  
  13.     Written by: Pete Gontier    
  14.  
  15.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  16.  
  17.                 You may incorporate this Apple sample source code into your program(s) without
  18.                 restriction. This Apple sample source code has been provided "AS IS" and the
  19.                 responsibility for its operation is yours. You are not permitted to redistribute
  20.                 this Apple sample source code as "Apple sample source code" after having made
  21.                 changes. If you're going to re-distribute the source, we require that you make
  22.                 it clear in the source that the code was descended from Apple sample source
  23.                 code, but that you've made changes.
  24.  
  25.     Change History (most recent first):
  26.                 8/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  27.                 02/26/97    PG                Somehow the files on the CD had become
  28.                                             confused. Verified everything still works.
  29.  
  30.                 03/25/96    PG                Was calling DeleteMenu with magic number.
  31.                                             Fixed bizarre menu bar offset problem.
  32.                                             Thanks to Harold Ekstrom.
  33.     
  34.                 
  35. */
  36.  
  37. #define SystemSevenOrLater        1
  38. #define CGLUESUPPORTED            0
  39. #define OLDROUTINENAMES            0
  40. #define OLDROUTINELOCATIONS        0
  41.  
  42. #ifndef __FONTS__
  43. #    include <Fonts.h>
  44. #endif
  45.  
  46. #ifndef __DIALOGS__
  47. #    include <Dialogs.h>
  48. #endif
  49.  
  50. #ifndef __RESOURCES__
  51. #    include <Resources.h>
  52. #endif
  53.  
  54. #ifndef __LOWMEM__
  55. #    include <LowMem.h>
  56. #endif
  57.  
  58.     //
  59.     //    HOSTED_BY_FONT_MISCREANT
  60.     //
  61.     //    If you are writing an external code resource (filter,
  62.     //    XCMD, etc.), and this code isn't giving you any joy,
  63.     //    let HOSTED_BY_FONT_MISCREANT be 1 and see if that helps.
  64.     //    Some extension hosts set up a hostile font environment.
  65.     //    Your lack of joy is their fault, and they force you
  66.     //    to assume even more compatibility risk than you would
  67.     //    by using this code without HOSTED_BY_FONT_MISCREANT
  68.     //    defined. On the other hand, we *are* weaving a tangled
  69.     //    web by twiddling low memory, and sometimes one must pay
  70.     //    the price for this sort of behavior.
  71.     //
  72.  
  73. #define HOSTED_BY_FONT_MISCREANT 0
  74.  
  75. #if HOSTED_BY_FONT_MISCREANT
  76.  
  77. static pascal GrafPtr GetSomeWindowManagerPort (void)
  78. {
  79.     //
  80.     //    Produces a pointer to the Window Manager port or
  81.     //    Color Window Manager Port. You can test the returned
  82.     //    GrafPtr to see whether it's colored or not, but
  83.     //    many callers won't care.
  84.     //
  85.  
  86.     GrafPtr result = nil;
  87.  
  88.     SysEnvRec theWorld;
  89.  
  90.     //
  91.     //    Yes, SysEnvirons is deprecated. But it still works,
  92.     //    and it's a cheap way to buy backward compatibility
  93.     //    if you only need to know whether CQD support exists.
  94.     //
  95.  
  96.     if (SysEnvirons (1, &theWorld))
  97.         DebugStr ("\pWhoa! Panic! SysEnvirons failed?!");
  98.     else if (theWorld.hasColorQD)
  99.         GetCWMgrPort ((CGrafPtr *) &result);
  100.     else
  101.         GetWMgrPort (&result);
  102.  
  103.     return result;
  104. }
  105.  
  106. #endif
  107.  
  108. static pascal void PopUpMenuSelectWithCurFont
  109.     (MenuRef popUpMenuRef, Point where, unsigned short prevSelection)
  110. {
  111.     GrafPtr hostPort;
  112.  
  113.     short oldSysFont = LMGetSysFontFam ( );
  114.     short oldSysSize = LMGetSysFontSize ( );
  115.  
  116.     GetPort (&hostPort);
  117.  
  118.     //
  119.     //    Believe it or not, it's important to insert
  120.     //    the menu before diddling the font characteristics.
  121.     //
  122.  
  123.     InsertMenu (popUpMenuRef,hierMenu);
  124.  
  125.     LMSetSysFontFam (hostPort->txFont);
  126.     LMSetSysFontSize (hostPort->txSize);
  127.     LMSetLastSPExtra (-1);
  128.  
  129. #if HOSTED_BY_FONT_MISCREANT
  130.     SetPort (GetSomeWindowManagerPort ( ));
  131.     TextFont (0);
  132.     TextSize (0);
  133.     SetPort (hostPort);
  134. #endif
  135.  
  136.     PopUpMenuSelect (popUpMenuRef, where.v, where.h, prevSelection);
  137.  
  138.     LMSetSysFontFam (oldSysFont);
  139.     LMSetSysFontSize (oldSysSize);
  140.     LMSetLastSPExtra (-1);
  141.  
  142.     DeleteMenu ((**popUpMenuRef).menuID);
  143. }
  144.  
  145. //////////////////////////////////////////////////////////////////////
  146. //
  147. //    Below please find the usual sort of application boilerplate.
  148. //
  149. //////////////////////////////////////////////////////////////////////
  150.  
  151. static pascal OSErr InitMac (void)
  152. {
  153.     MaxApplZone ( );
  154.     InitGraf (&(qd.thePort));
  155.     InitFonts ( );
  156.     InitWindows ( );
  157.     InitMenus ( );
  158.     TEInit ( );
  159.     InitDialogs (nil);
  160.  
  161.     return noErr;
  162. }
  163.  
  164. static pascal Boolean ModalFilterProc (DialogPtr theDialog, EventRecord *theEvent, short *)
  165. {
  166.     Boolean result = false;
  167.  
  168.     if (theEvent->what == mouseDown)
  169.     {
  170.         Point localWhere = theEvent->where;
  171.         GrafPtr savePort;
  172.     
  173.         GetPort (&savePort);
  174.         SetPort (theDialog);
  175.         GlobalToLocal (&localWhere);
  176.     
  177.         if (FindDialogItem (theDialog, localWhere) == 1)
  178.         {
  179.             MenuRef popUpMenuRef = GetMenu (128);
  180.             if (popUpMenuRef)
  181.             {
  182.                 short txFont = theDialog->txFont;
  183.                 short txSize = theDialog->txSize;
  184.                 short iType; Handle iHandle; Rect iRect;
  185.                 Point popWhere;
  186.  
  187.                 GetDialogItem (theDialog,2,&iType,&iHandle,&iRect);
  188.  
  189.                 popWhere.v = iRect.bottom;
  190.                 popWhere.h = iRect.left + 2;
  191.                 LocalToGlobal (&popWhere);
  192.  
  193.                 InvertRect (&iRect);
  194.                     TextFont (kFontIDGeneva);
  195.                         TextSize (9);
  196.                             PopUpMenuSelectWithCurFont (popUpMenuRef,popWhere,0);
  197.                         TextSize (txSize);
  198.                     TextFont (txFont);
  199.                 InvertRect (&iRect);
  200.  
  201.                 ReleaseResource ((Handle) popUpMenuRef);
  202.             }
  203.         }
  204.     
  205.         SetPort (savePort);
  206.     }
  207.  
  208.     return result;
  209. }
  210.  
  211. static pascal Boolean SetUpMenuBar (void)
  212. {
  213.     Boolean result = false;
  214.     Handle mBar = GetNewMBar (128);
  215.     if (!ResError ( ) && mBar)
  216.     {
  217.         SetMenuBar (mBar);
  218.         AppendResMenu (GetMenuHandle (130), 'DRVR');
  219.         DrawMenuBar ( );
  220.         result = true;
  221.         ReleaseResource (mBar);
  222.     }
  223.     return result;
  224. }
  225.  
  226. void main (void)
  227. {
  228.     if (!InitMac ( ) && SetUpMenuBar ( ))
  229.     {
  230.         DialogRef dlgRef = GetNewDialog (128,nil,nil);
  231.         if (dlgRef)
  232.         {
  233.             short itemHit;
  234.             ModalDialog (NewModalFilterProc(ModalFilterProc),&itemHit);
  235.             DisposeDialog (dlgRef);
  236.         }
  237.     }
  238. }
  239.